package user.zc.util;

import user.zc.config.IConfig;
import user.zc.config.system.config.SystemColumnConfig;
import user.zc.config.system.config.SystemOutputFileConfig;
import user.zc.config.system.template.LocalTemplateConfig;
import user.zc.config.user.config.ConnectionConfig;
import user.zc.config.user.config.UserColumnConfig;
import user.zc.config.user.config.UserConfig;
import user.zc.config.user.config.UserOutputFileConfig;
import user.zc.config.value.ColumnValue;
import user.zc.config.value.Key;
import user.zc.config.value.KeyValue;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.util.Map;

/**
 * @author zhouchuang
 * @date 2023/8/8 16:59
 */
public class ConfigGroupHelper {
    private ConfigMap< IConfig> configs = new ConfigMap< >();
    private static class GlobalConstantsInstance {
        private static final ConfigGroupHelper inner = new ConfigGroupHelper();
    }

    public static ConfigGroupHelper getInst(){
        return GlobalConstantsInstance.inner;
    }



    private ConfigGroupHelper(){
        try {
            configs.add(new ConnectionConfig());
            configs.add(new UserColumnConfig());
            configs.add(new UserOutputFileConfig());

            configs.add(new SystemColumnConfig());
            configs.add(new SystemOutputFileConfig());
            LocalTemplateConfig localTemplateConfig = new LocalTemplateConfig();
            configs.add(localTemplateConfig);
            configs.addAll(localTemplateConfig.loadConfig());
        } catch (FileNotFoundException e){

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean saveConnection(String fileName, Map<String, KeyValue> map)throws Exception{
        //保存完毕后，需要检查是新增还是修改操作，如果新增，需要复制其他配置过来
        boolean isNew ;
        if( isNew = get(ConnectionConfig.class).saveOrUpdate(fileName,map)){

            Map<String, ColumnValue>  columnValueMap = get(SystemColumnConfig.class).getData();
            get(UserColumnConfig.class).saveOrUpdate(fileName,columnValueMap);

            Map<String, Key>  keyMap = get(SystemOutputFileConfig.class).getData();
            get(UserOutputFileConfig.class).saveOrUpdate(fileName,keyMap);
        }
        return isNew;
    }

    public void switchConnection(String fileName) throws Exception{
        //更新用户配置的所有信息
        configs.values().stream().filter(t-> UserConfig.class.isAssignableFrom(t.getClass())).forEach(cfg-> {
            try {
                cfg.loadConfig(fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public  void deleteConection(String fileName) throws Exception {
        deleteFold(System.getProperty("user.home")+File.separator+ IConfig.FOLD+File.separator+fileName);
    }

    private void deleteFold(String path)throws Exception{
        File tempDir = new File(path);
        tempDir.setWritable(true);
        if (tempDir.isDirectory()) {
            File[] files = tempDir.listFiles();
            for (File f : files) {
                deleteFold(f.getPath());
            }
        }
        Files.delete(tempDir.toPath());
    }

    public  <T extends IConfig> T get(Class<T> clazz){
        if(configs.containsKey(clazz)){
            return (T)configs.get(clazz);
        }else{
            throw new IllegalArgumentException("找不到名称为【"+clazz.getSimpleName()+"】的配置类");
        }

    }

    public  <T extends IConfig> T get(String className){
        if(configs.containsKey(className)){
            return (T)configs.get(className);
        }else{
            throw new IllegalArgumentException("找不到名称为【"+className+"】的配置类");
        }
    }

}
